<?php
/*
 * 小组权限判断断言
 *
 */
class Acl_Assert_Group implements Acl_Assert_Interface 
{
	public function __construct()
	{
		//
	}
	
	public function assert(Acl_Core $acl, $role = null, $resource = null, $privilege = null)
	{
		$res = $resource->get_resource_id();
		switch ($res)
		{
			case 'subject': 
				return $this->subject_assert($role, $resource, $privilege);
				break;

			case 'group' :
				return $this->group_assert($role, $resource, $privilege);
				break;

			case 'bbs' :
				return $this->bbs_assert($role, $resource, $privilege);
				break;

			default :
				return FALSE;
				break;
		}
	}

	private function subject_assert ($role, $resource, $privilege)
	{
		$group = $resource->group();
		$type  = $group->get_type();
		$uid = $role->get_user_id();
		$subject = $resource->subject_info();
		$public = 0;

		if ($privilege === 'reply')
		{
			$public = Kohana::config('group.type.public_post_reply');
		}
		else if ($privilege === 'edit')
		{
			return FALSE;
		}
		else if ($privilege === 'read')
		{
			$public = Kohana::config('group.type.public_list_subject');
		}
		else if ($privilege === 'remove')
		{
		        if($group->is_manager($role)||$uid == $subject['uid']||Group_User::is_bbs_manager($role))
				 return TRUE;
				else
				return FALSE;
		}

		if ($type & $public)
			return TRUE;
		else if ($group->has_member($role))
			return TRUE;
		else
			return FALSE;
	}

	private function group_assert ($role, $resource, $privilege)
	{
		$priv  = Kohana::config('group.type.public_reply_subject');
		$group =& $resource;
		$type  = $group->get_type();

		if ($privilege === 'create_subject')
		{
			$public = Kohana::config('group.type.public_post_subject');
		}
		else if ($privilege === 'list_subject')
		{
			$public = Kohana::config('group.type.public_list_subject');
		}
		else if ($privilege === 'edit')
		{
			if ($group->is_manager ($role)||Group_User::is_bbs_manager ($role))
				return TRUE;
			else
				return FALSE;
		}
		else if ($privilege === 'view_info')
		{
			$public = Kohana::config('group.type.public_group_info');
		}

		if ($type & $public)
			return TRUE;
		else if ($group->has_member($role))
			return TRUE;
		else
			return FALSE;
	}

	private function bbs_assert ($role, $resource, $privilege)
	{
		$bbs   =& $resource;

		if ($privilege === 'create_group')
		{
			if (Group_User::is_bbs_manager ($role))
				return TRUE;
			else
				return FALSE;
		}
		else
			return FALSE;
	}
}
